<?php


namespace app\official\model;


use app\common\model\BaseModel;

class UserExperience extends BaseModel {

    /**
     * 添加/编辑
     * @param $params
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        $ex = db("user_experience")->where('phone', $params['phone'])->find();
        if ($ex) {
            db("user_experience")->where('phone', $params['phone'])->update(['last_time' => time()]);
        } else {
            $region1 = db('region')->where('city_code', $params['city_code'])->field('p_code,name')->find();
            $region2 = db('region')->where('city_code', $region1['p_code'])->field('city_code,name')->find();
            $insert = [
                'phone'      => $params['phone'],
                'city_codes' => $region2['city_code'] . ',' . $params['city_code'],
                'city_names' => $region2['name'] . '-' . $region1['name'],
                'is_do'      => 0,
                'itime'      => time(),
                'do_time'    => 0,
                'last_time'  => time(),
            ];
            db("user_experience")->insert($insert);
        }
    }

}